var fs = require('fs'),
    PNG = require('pngjs').PNG;
  
let b = [];
let r_g = [];
let all = [];
let asciiStr = '';
 
fs.createReadStream('in.png')
    .pipe(new PNG({
        filterType: 4
    }))
    .on('parsed', function() {
 
        for (var y = 0; y < this.height; y++) {
            for (var x = 0; x < this.width; x++) {
                var idx = (this.width * y + x) << 2;
 
                // invert color
                // this.data[idx] = 255 - this.data[idx];
                // this.data[idx+1] = 255 - this.data[idx+1];
                // this.data[idx+2] = 255 - this.data[idx+2];
                if (this.data[idx+3] < 255) {
                  // this.data[idx] = (this.data[idx+3] - 253)*255;
                  // this.data[idx+1] = (this.data[idx+3] - 253)*255;
                  // this.data[idx+2] = (this.data[idx+3] - 253)*255;
                  
                  // this.data[idx] = this.data[idx]
                  // this.data[idx+1] = this.data[idx+1]
                  // this.data[idx+2] = this.data[idx+2]
                  if (y == '310') {
                    b.push(254 - this.data[idx+3]);
                    all.push({
                      r: this.data[idx],
                      g: this.data[idx+1],
                      b: this.data[idx+2],
                      a: this.data[idx+3]
                    })
                    if (this.data[idx] - this.data[idx+1] != 0) {
                      console.log(this.data[idx] - this.data[idx+1])
                    }
                    let a= {
                      '-1': 'v',
                      '0': '-',
                      '1': '^',
                    }
                    console.log(this.data[idx] - this.data[idx+1])
                    r_g.push(a[this.data[idx] - this.data[idx+1]])
                  }
                } else if (this.data[idx+3] < 255) {
                  this.data[idx] = this.data[idx]
                  this.data[idx+1] = this.data[idx+1]
                  this.data[idx+2] = this.data[idx+2]
                  
                  if (y == '310') {
                  console.log(this.data[idx], this.data[idx+1], this.data[idx+2])
                  }
                } else {
                  this.data[idx] = 0
                  this.data[idx+1] = 0
                  this.data[idx+2] = 0
                }
                // if (this.data[idx+3] != 255) {
                //   console.log(this.data[idx+3])
                // }
                // and reduce opacity
                this.data[idx+3] = 255;
            }
        }

        // for (var i = 0; i < b.length; i+=8) {
        //   let octStr = 128 * b[i] + 64 * b[i + 1] + 32 * b[i + 2] + 16 * b[i + 3] + 8 * b[i + 4] + 4 * b[i + 5] + 2 * b[i + 6] + 1 * b[i + 7];
        //   let octStr2 = 128 * r_g[i] + 64 * r_g[i + 1] + 32 * r_g[i + 2] + 16 * r_g[i + 3] + 8 * r_g[i + 4] + 4 * r_g[i + 5] + 2 * r_g[i + 6] + 1 * r_g[i + 7];
        //   // if (octStr>128) {
        //     asciiStr+=String.fromCharCode(octStr2);
        //   // }
        //   // console.log(String.fromCharCode(octStr), octStr, b[i], r_g[i]); 
        // }
        fs.writeFileSync('r_g.b.txt', r_g.join(''))
        // fs.writeFileSync('r_g.txt', asciiStr)
        this.pack().pipe(fs.createWriteStream('out2.png'));
    });
